<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
        <el-row>
          <el-col :span="6">
            <el-form-item label="发票单号" prop="billId">
              <el-input v-model="dataForm.billId" placeholder="发票单号" size="mini"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="制造商">
              <el-input v-model="dataForm.supplyName" style="width: 200px">
                <el-button slot="append" @click="openDialog('supply')">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态">
              <el-select v-model="dataForm.status">
                <el-option label="已审核" value="1"></el-option>
                <el-option label="未审核" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发票类型">
              <el-select v-model="dataForm.type">
                <el-option label="采购普通发票" value="1"></el-option>
                <el-option label="采购增值税发票" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="发票日期">
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="审核日期">
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.auditTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.auditTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="制作人工号" prop="createWorkNumber">
          <el-input v-model="dataForm.createWorkNumber" placeholder="制作人工号"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24">
          <el-button v-if="permissions.eims_invoice_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column label="操作" width="170">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" @click="lookDetail(scope.row.id)">详情</el-button>
              <el-button v-if="permissions.eims_invoice_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)" :disabled="scope.row.status ==1">修改</el-button>
              <el-button v-if="permissions.eims_invoice_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row)"  :disabled="scope.row.status ==1">删除</el-button>
              <el-button v-if="permissions.eims_invoice_edit" type="text" icon="el-icon-edit" @click="editAudit(scope.row)" :disabled="scope.row.status ==0">反审核</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="billId" label="发票号" width="170"></el-table-column>
          <el-table-column prop="supplyName" label="制造商"></el-table-column>
          <el-table-column prop="createTime" label="发票日期"></el-table-column>
          <el-table-column prop="createUserName" label="制作人"></el-table-column>
          <el-table-column prop="costPriceTotal" label="成本合计"></el-table-column>
          <el-table-column prop="taxPriceTotal" label="税额合计"></el-table-column>
          <el-table-column prop="allPriceTotal" label="价税合计"></el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next"
        ></el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <!-- 弹窗, 详情页 -->
      <detail-form v-if="detailVisible" ref="detailInfo" @refreshDataList="getDataList"></detail-form>
      <!-- 弹窗, 制造商列表  -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyId"></search-supply>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj,cancelAudit } from "@/api/eims/invoice";
import TableForm from "./invoice-form";
import DetailForm from "./invoice-detail";
import { filterParams } from "@/util/tools";
import SearchSupply from '@/components/business/SearchSupply'
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      searchShow: true,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      supplyVisible: false,
      detailVisible:false
    };
  },
  components: {
    TableForm,
    SearchSupply,
    DetailForm
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let params1 = filterParams(this.dataForm)
      let params2 = {
        current: this.pageIndex,
        size: this.pageSize
      }
      fetchList(Object.assign(params2, params1)).then((response) => {
        if(response.data.code == '0'){
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        }else{
          this.$message.error(response.data.msg)
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    //反审核
    editAudit(row){
      this.$confirm("是否确认反审核发票号为" + row.billId, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return cancelAudit(row.id)
        })
        .then((response) => {
          if(response.data.code == '0'){
            this.$message.success("反审核成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
    },
    // 查看详情
    lookDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.detailInfo.init(id);
      });
    },
    // 删除
    deleteHandle(row) {
      this.$confirm("是否确认删除发票号为" + row.billId, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return delObj(row.id);
      }).then((response) => {
        if(response.data.code == '0'){
          this.$message.success("删除成功")
          this.getDataList()
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    searchChange() {
      this.pageIndex = 1
      this.getDataList()
    },
    // 搜索清空
    searchReset() {
      for (var key in this.dataForm) {
        this.dataForm[key] = ''
      }
    },
    handleSearchShow() {
      this.searchShow = !this.searchShow
    },
    /**
     * 刷新回调
     */
    refreshChange() {
      this.getDataList(this.page)
    },
    //打开制造商查询弹窗
    openDialog(type) {
      if(type == 'supply'){
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init({})
        })
      }
    },
    getSupplyId(data) {
      this.$set(this.dataForm,'supplyName',data.name)
      this.dataForm.supplyId = data.id
    },
  },
};
</script>
